package Client.GUI;
/**
 * 
 * /**
 * 
 * @author Baglama Team
 *
 *
 */
import Client.logic.ClientIF;
import Server.DataBase.Lesson;
import Server.DataBase.Student;
import Server.Message.MessageAssesmentFrom;
import Server.Message.MessageGetStudentsInLesson;
import Server.Message.MessageGetStudentsReplay;
import Server.Message.MessageLessonReplay;
import Server.Message.MessageTeacherLesson;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JButton;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/**

teacher initializes AssesmentForm add task panel
 */
public class AssesmentForm extends MyJPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField textFieldGrade;
	private JComboBox comboBoxLesson;
	private JComboBox comboBoxStudent;
	private JButton btnApplay;
	private Lesson lesson;
	private Student student;
	private JTextPane textPaneCom;
	
	public AssesmentForm(ClientIF client) {
		super(PanelType.ASSESMENT_FORM, client);
		BufferedImage myPic;
		try {
			myPic = ImageIO.read(new File("images"+ File.separator+"assessment form.jpg"));
		
			JLabel lblAddClass = new JLabel(new ImageIcon(myPic), JLabel.CENTER);
			lblAddClass.setBounds(85, 7, 398, 99);
			add(lblAddClass);
		
		} catch (IOException e) {
			e.printStackTrace();
		}
		setLayout(null);
		init();
		
		
		
	}
	
	public void initBtn(){
		btnApplay = new JButton("Apply");
		btnApplay.setBounds(283, 335, 89, 23);
		btnApplay.setEnabled(false);
		btnApplay.addActionListener(new ApplyListener());
		add(btnApplay);
	}
	
	/**
	  
	teacher initializes AssesmentForm panel
	 */
	public void initLabels(){
		
		JLabel lblLesson = new JLabel("Lesson:");
		lblLesson.setBounds(121, 119, 59, 14);
		add(lblLesson);
		
		JLabel lblStudent = new JLabel("student:");
		lblStudent.setBounds(121, 157, 59, 14);
		add(lblStudent);
		

		JLabel lblFinalGrade = new JLabel("Final Grade:");
		lblFinalGrade.setBounds(121, 199, 75, 14);
		add(lblFinalGrade);
		
		JLabel lblComment = new JLabel("Comment:");
		lblComment.setBounds(121, 240, 75, 14);
		add(lblComment);
	}
	/**
	  
	teacher initializes AssesmentForm panel
	 */
	public void initComboBox(){
		comboBoxLesson = new JComboBox();
		comboBoxLesson.setBounds(190, 116, 90, 20);
		add(comboBoxLesson);
		
		 comboBoxStudent = new JComboBox();
		 comboBoxStudent.setEnabled(false);
		comboBoxStudent.setBounds(190, 154, 90, 20);
		add(comboBoxStudent);
		
	}

	public void initTextField(){
		textFieldGrade = new JTextField();
		textFieldGrade.setBounds(190, 196, 86, 20);
		add(textFieldGrade);
		textFieldGrade.setColumns(10);
		
		textPaneCom = new JTextPane();
		textPaneCom.setBorder(new LineBorder(new Color(0, 0, 0)));
		textPaneCom.setBounds(190, 234, 224, 93);
		add(textPaneCom);
		
	}
	
	public void init(){
		initLabels();
		initBtn();
		initComboBox();
		initTextField();
		getClient().sendMsgToServer(new MessageTeacherLesson(getClient().getUser().getIdUser()));
		MessageLessonReplay msg = (MessageLessonReplay) getClient().getMessageFromServer();
	
		comboBoxLesson.addItem("Choose");
		for (Lesson lesson : msg.getArray())
			comboBoxLesson.addItem(lesson);
		comboBoxLesson.addActionListener(new LessonListener());
	}
	
	private class LessonListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			lesson = (Lesson) comboBoxLesson.getSelectedItem();	
			
			getClient().sendMsgToServer(new MessageGetStudentsInLesson(lesson.getIdLesson()));
			MessageGetStudentsReplay msg2 = (MessageGetStudentsReplay) getClient().getMessageFromServer();
			comboBoxStudent.removeAllItems();
			comboBoxStudent.setEnabled(true);
			comboBoxStudent.addItem("Choose");
			for (Student student : msg2.getArray()) 
				comboBoxStudent.addItem(student);
			comboBoxStudent.addActionListener(new StudentListener());
			comboBoxLesson.setEnabled(false);
		}
	}
	
	private class StudentListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			student =   (Student) comboBoxStudent.getSelectedItem();
			btnApplay.setEnabled(true);
			comboBoxStudent.setEnabled(false);
		}
	}
	
	private class ApplyListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			if (checkGrade()){
				int grade = Integer.parseInt(textFieldGrade.getText());
				getClient().sendMsgToServer(new MessageAssesmentFrom
					(textPaneCom.getText(), grade, student.getIdUser(), lesson.getIdLesson()));
				getClient().swapFromBack(new AssesmentForm(getClient()));
			}
			else JOptionPane.showMessageDialog((Component) getClient(), "Wrong Grade");
		}
	}
	
	private boolean checkGrade(){
		try{
			String str = textFieldGrade.getText();
			int g = Integer.parseInt(str);
			if (g>=0 && g<=100) return true;
		} catch (java.lang.Throwable t) {}
		return false;
	}

	/**
	 *Method activate back panel 
	 * */
	public MyJPanel pushPanel() {
		return new AssesmentForm(getClient());
		
}}
